package com.pavelk.orion.model
{
	import flash.utils.describeType;
	
	import flash.text.Font;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.system.LoaderContext;
	import flash.system.ApplicationDomain;
	import flash.net.URLRequest;
	import flash.events.*;
		
    import org.puremvc.as3.interfaces.IProxy;
    import org.puremvc.as3.patterns.proxy.Proxy;
    import org.puremvc.as3.utilities.loadup.interfaces.ILoadupProxy;
    
    import com.pavelk.orion.ApplicationFacade;
	
	public class RuntimeFontsProxy extends EntityProxy implements ILoadupProxy
	{
		public static const NAME  :String = "RuntimeFontsProxy";
        public static const SRNAME:String = "RuntimeFontsSRProxy";
		
		public function RuntimeFontsProxy()
		{
			super( NAME );
		}
		
		public function load() : void
		{
			sendNotification( ApplicationConstants.FONTS_LOADING );
			
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, runtimeFontsLoadComplete);
			loader.load( new URLRequest("FontsAssets.swf") );
		}
		
		private function runtimeFontsLoadComplete( e:Event ) : void 
		{				
			var FontLibrary:Class   = e.target.applicationDomain.getDefinition("FontLibrary") as Class;			
			var description:XMLList = describeType(FontLibrary)..variable;

			for( var i:int = 0; i < description.length(); i++ )
            {
                Font.registerFont( FontLibrary[ description[ i ].@name ] );                
            }

			sendLoadedNotification( ApplicationConstants.FONTS_LOADED, NAME, SRNAME );
		}
	}
}